package com.cskaoyan.service;

import com.cskaoyan.exception.GoodsIdNonexistentException;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bo.extend.GrouponBo;
import com.cskaoyan.model.bo.extend.GrouponRulesBo;
import com.cskaoyan.model.vo.extend.GrouponRulesVo;
import com.cskaoyan.model.vo.extend.GrouponVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：Chunk
 * @Description:
 * @Date Created in 12:30 2021/4/10
 */
@Service
public class GrouponServiceImpl implements GrouponService {
    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public GrouponRulesVo grouponList(GrouponRulesBo grouponRulesBo) {
        PageHelper.startPage(grouponRulesBo.getPage(), grouponRulesBo.getLimit());
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();

        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        if(grouponRulesBo.getGoodsId() != null && !"".equals(grouponRulesBo.getGoodsId())){
            criteria.andGoodsIdEqualTo(grouponRulesBo.getGoodsId());
        }

        grouponRulesExample.setOrderByClause(grouponRulesBo.getSort() + " " + grouponRulesBo.getOrder());
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);

        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();

        GrouponRulesVo grouponRulesVo = new GrouponRulesVo();
        grouponRulesVo.setTotal(total);
        grouponRulesVo.setItems(grouponRules);
        return  grouponRulesVo;
    }

    @Override
    public GrouponRules grouponCreate(GrouponRules grouponRules) throws GoodsIdNonexistentException {
        addGoodsMsg(grouponRules);
        grouponRulesMapper.insertSelective(grouponRules);
        GrouponRules grouponRulesVo = grouponRulesMapper.selectByPrimaryKey(grouponRules.getId());
        return grouponRulesVo;
    }

    @Override
    public GrouponRules grouponUpdate(GrouponRules grouponRules) throws GoodsIdNonexistentException {
        addGoodsMsg(grouponRules);
        grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
        GrouponRules grouponRulesVo = grouponRulesMapper.selectByPrimaryKey(grouponRules.getId());
        return grouponRulesVo;
    }

    @Override
    public void grouponDelete(GrouponRules grouponRules) {
        grouponRules.setDeleted(true);
        grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
    }

    @Override
    public GrouponVo grouponListRecord(GrouponBo grouponBo) {
        PageHelper.startPage(grouponBo.getPage(),grouponBo.getLimit());
        GrouponExample grouponExample = new GrouponExample();

        GrouponExample.Criteria criteria = grouponExample.createCriteria();

        grouponExample.setOrderByClause(grouponBo.getSort() + " " + grouponBo.getOrder());
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);

        PageInfo<Groupon> grouponPageInfo = new PageInfo<>(groupons);
        long total = grouponPageInfo.getTotal();

        GrouponVo grouponVo = new GrouponVo();
        grouponVo.setTotal(total);
        grouponVo.setItems(groupons);
        return grouponVo;
    }

    private void addGoodsMsg(GrouponRules grouponRules) throws GoodsIdNonexistentException {
        Goods goods = null;
        goods = goodsMapper.selectByPrimaryKey(grouponRules.getGoodsId());
        if(goods == null){
            throw new GoodsIdNonexistentException();
        }
        String picUrl = goods.getPicUrl();
        String name = goods.getName();
        grouponRules.setGoodsName(name);
        grouponRules.setPicUrl(picUrl);
    }
}
